package com.onetime.mall.product.controller;

import com.onetime.common.utils.PageUtils;
import com.onetime.common.utils.R;
import com.onetime.mall.product.entity.AttrEntity;
import com.onetime.mall.product.entity.AttrGroupEntity;
import com.onetime.mall.product.service.AttrAttrgroupRelationService;
import com.onetime.mall.product.service.AttrGroupService;
import com.onetime.mall.product.service.AttrService;
import com.onetime.mall.product.service.CategoryService;
import com.onetime.mall.product.vo.AttrGroupWithAttrsVo;
import com.onetime.mall.product.vo.AttrRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author liyixuan
 * @email liyixuan106@163.com
 * @date 2022-03-06 17:13:55
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 获取分类下所有分组&关联属性
     *
     * @return json
     */
    //    /product/attrgroup/{catelogId}/withattr
    @RequestMapping("/{catelogId}/withattr")
    public R getAttributeGroupingAndRelatedAttributes(@PathVariable("catelogId") Long catelogId) {
        /*查出当前分类下的所有属性分组*/
        /*查询每个分组的所有属性*/
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttributeGroupingAndRelatedAttributesByCategoryID(catelogId);
        return R.ok().put("data", vos);
    }

    /**
     * 新增关联关系
     */
    //    /product/attrgroup/attr/relation
    @RequestMapping("/attr/relation")
    public R addRelated(@RequestBody List<AttrRelationVo> vos) {
        relationService.saveBatch(vos);

        return R.ok();
    }

    /**
     * 属性关联
     *
     * @param attrgroupId 属性分组ID
     * @return JSON
     */
    //    /product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attributeAssociation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.GetRelevantAttributes(attrgroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 获得没有关联的属性列表
     *
     * @param attrgroupId 属性分组ID
     * @return JSON
     */
    //    /product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attributeNoAssociation(@PathVariable("attrgroupId") Long attrgroupId,
                                    @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.GetRelevantNoAttributes(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        /*PageUtils page = attrGroupService.queryPage(params);*/
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        /*查出具体属性分类*/
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        /*得到当前属性的路径*/
        Long CategoryID = attrGroup.getCatelogId();
        /*根据当前路径找到完整路径*/
        Long[] path = categoryService.findCategoryPath(CategoryID);
        /*在属性分组中放置完整路径*/
        attrGroup.setCatelogPath(path);
        System.out.println("完整路径：" + Arrays.asList(path));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    //    /product/attrgroup/attr/relation/delete
    @RequestMapping("/attr/relation/delete")
    public R deleteRelated(@RequestBody AttrRelationVo[] attrRelationVos) {
        attrService.deleteRelated(attrRelationVos);
        return R.ok();
    }

}
